package com.lqk.files;


import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class FilesDemo {
    public static void main(String[] args) throws Exception {
        // Files.createDirectory()
//        Path path = Paths.get("D:\\Progrom Files\\javaProject\\nioProj\\lqk\\test");

//        Path directory = Files.createDirectory(path);

        //copy()
//        Path sourcePath = Paths.get("D:\\Progrom Files\\javaProject\\nioProj\\lqk\\001.txt");
//        Path destinationPath = Paths.get("D:\\Progrom Files\\javaProject\\nioProj\\lqk\\test\\001.txt");

//        Files.copy(sourcePath,destinationPath);
        // 覆盖已经存在的文件
//        Files.copy(sourcePath,destinationPath, StandardCopyOption.REPLACE_EXISTING);

        // move() 文件移动或重命名
//        Path sourcePath = Paths.get("D:\\Progrom Files\\javaProject\\nioProj\\lqk\\001.txt");
//        Path destinationPath = Paths.get("D:\\Progrom Files\\javaProject\\nioProj\\lqk\\0001.txt");
//        Files.move(sourcePath,destinationPath,StandardCopyOption.REPLACE_EXISTING);

        // delete() 删除操作
//        Path deletePath = Paths.get("D:\\Progrom Files\\javaProject\\nioProj\\lqk\\test\\001.txt");
//        Files.delete(deletePath);

        // Files.walkFileTree()
        Path rootPath = Paths.get("D:\\Progrom Files\\javaProject\\nioProj");
        String fileToFind = File.separator + "001.txt";
        try {
            Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws
                        IOException {
                    String fileString = file.toAbsolutePath().toString();
                    //System.out.println("pathString = " + fileString);
                    if(fileString.endsWith(fileToFind)){
                        System.out.println("file found at path: " + file.toAbsolutePath());
                        return FileVisitResult.TERMINATE;
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch(IOException e){
            e.printStackTrace();
        }


    }
}
